package client.view;


import client.FriendListManager;

import client.service.LoginUser;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Stage;
import javafx.stage.StageStyle;
import model.Message;
import model.MsgType;
import client.service.UpdateFriendListConnector;


//客户端登录页面,点击登录按钮实现按钮，已实现用户身份校验
public class Login extends Application {
    private ImageView jlb_north;//北部背景图片标签
    private Label acclabel;
    private Label pwdlabel;
    private TextField qqNum;//账号输入框
    private PasswordField qqPwd;//密码输入框
    private Hyperlink btn_link;//账号输入框后的"注册账号"
    private Button btn_login;//南部登录按钮
    private Button btn_min;
    private Button btn_exit;
    boolean isDragged = false;//记录鼠标是否是拖拽移动
    private double xOffset = 0;
    private double yOffset = 0;
    public void start(Stage primaryStage) {
        AnchorPane root = new AnchorPane();
        primaryStage.initStyle(StageStyle.UNDECORATED);
        // 处理北部背景图片标签
        Image northImg = new Image("file:image/login/login.jpg", 430, 182, false, false);
        jlb_north = new ImageView(northImg);
        root.getChildren().add(jlb_north);

        btn_min = new Button();
        ImageView minView = new ImageView(new Image("file:image/login/min.jpg"));
        btn_min.setGraphic(minView);
        AnchorPane.setTopAnchor(btn_min, 0.0);
        AnchorPane.setLeftAnchor(btn_min, 360.0);
        btn_min.setStyle("-fx-background-color: transparent;"); // 设置样式
        btn_min.setOnAction(e -> primaryStage.setIconified(true));
        root.getChildren().add(btn_min);

        btn_exit = new Button();
        ImageView exitView = new ImageView(new Image("file:image/login/exit.png"));
        btn_exit.setGraphic(exitView);
        AnchorPane.setTopAnchor(btn_exit, 0.0);
        AnchorPane.setLeftAnchor(btn_exit, 390.0);
        btn_exit.setStyle("-fx-background-color: transparent;"); // 设置样式
        btn_exit.setOnAction(e -> System.exit(0));
        root.getChildren().add(btn_exit);

        acclabel=new Label("账号");
        AnchorPane.setTopAnchor(acclabel, 200.0);
        AnchorPane.setLeftAnchor(acclabel, 90.0);
        root.getChildren().add(acclabel);

        //处理账号输入框
        qqNum = new TextField();
        AnchorPane.setTopAnchor(qqNum, 195.0);
        AnchorPane.setLeftAnchor(qqNum, 120.0);
        qqNum.setPrefWidth(194.0);
        qqNum.setPrefHeight(30.0);
        root.getChildren().add(qqNum);

        pwdlabel=new Label("密码");
        AnchorPane.setTopAnchor(pwdlabel, 245.0);
        AnchorPane.setLeftAnchor(pwdlabel, 90.0);
        root.getChildren().add(pwdlabel);

        //处理密码输入框
        qqPwd = new PasswordField();
        AnchorPane.setTopAnchor(qqPwd, 240.0);
        AnchorPane.setLeftAnchor(qqPwd, 120.0);
        qqPwd.setPrefWidth(194.0);
        qqPwd.setPrefHeight(30.0);
        root.getChildren().add(qqPwd);

        //处理账号输入框后的"注册账号"
        btn_link = new Hyperlink("注册账号");
        btn_link.setOnAction(e->{
            if(e.getSource() == btn_link){
                Register r = new Register();
                r.start(new Stage());
                primaryStage.close();
            }
        });
        AnchorPane.setTopAnchor(btn_link, 197.0);
        AnchorPane.setLeftAnchor(btn_link, 320.0);
        root.getChildren().add(btn_link);

        //处理南部登录按钮
        Image loginBtnImg = new Image("file:image/login/loginbutton.png");
        btn_login = new Button();
        btn_login.setGraphic(new ImageView(loginBtnImg));
        btn_login.setOnAction(e->{
            if(e.getSource() == btn_login){//点击登录
                String uid = qqNum.getText().trim();//获取输入账号
                String pwd = new String(qqPwd.getText());//获取密码
                //密码、账号的验证放在了model里面
                //接收检验结果
               Message mes = new LoginUser().sendLoginInfoToServer(uid , pwd);
                if(mes.getType() == MsgType.LOGIN_SUCCEED){

                    FriendList fl = new FriendList(mes.getTime(), uid, mes);//进入列表界面,调用了初始化函数
                    FriendListManager.addFriendListFrame(Integer.parseInt(uid), fl);
                    UpdateFriendListConnector upconn = new UpdateFriendListConnector(Integer.parseInt(uid));                                       
                    primaryStage.close();
                }
            }
        });
        AnchorPane.setTopAnchor(btn_login, 299.0);
        AnchorPane.setLeftAnchor(btn_login, 120.0);
        btn_login.setPrefWidth(194.0);
        btn_login.setPrefHeight(30.0);
        root.getChildren().add(btn_login);

        // 添加鼠标按下事件监听器
        root.setOnMousePressed(event -> {
            xOffset = event.getSceneX();
            yOffset = event.getSceneY();
        });

        // 添加鼠标拖拽事件监听器
        root.setOnMouseDragged(event -> {
            primaryStage.setX(event.getScreenX() - xOffset);
            primaryStage.setY(event.getScreenY() - yOffset);
        });


        Scene scene = new Scene(root, 430, 345);
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }
}